layui.use(['form', 'layer', 'laydate', 'element'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
        element = layui.element;
        laydate = layui.laydate;

    // //日期范围
    var beginTime = laydate.render({
        elem: '#beginTime'
        ,trigger: 'click'
        ,done: function (value, date, endDate) {
            endTime.config.min={
                year:date.year,
                month:date.month-1,//关键
                date: date.date,
                hours: 0,
                minutes: 0,
                seconds : 0
            }
        }
    });
    var endTime = laydate.render({
        elem: '#endTime'
        ,trigger: 'click'
        ,done: function (value, date, endDate) {
            beginTime.config.max={
                year:date.year,
                month:date.month-1,//关键
                date: date.date,
                hours: 0,
                minutes: 0,
                seconds : 0
            }
        }
    });

    form.on("submit(addOrUpdateTodo)", function (data) {
        data = data.field;

        if(data.beginTime =="undefined" || data.beginTime =="" || data.beginTime.trim()=="") {
            layer.msg('开始时间不能为空', {
                icon: 5
            });
            return false;
        }
        if(data.endTime =="undefined" || data.endTime =="" || data.endTime.trim()=="") {
            layer.msg('结束时间不能为空', {
                icon: 5
            });
            return false;
        }

        if(data.limitTime =="undefined" || data.limitTime =="" || data.limitTime.trim()=="") {
            layer.msg('时间限制不能为空', {
                icon: 5
            });
            return false;
        }

        index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
        //弹出loading
        var url=ctx + "/todo/add";
        if($("input[name='id']").val()){
            url=ctx + "/todo/update";
        }
        $.post(url, data, function (res) {
            if (res.code == 200) {
                setTimeout(function () {
                    top.layer.close(index);
                    top.layer.msg("操作成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                }, 500);
            } else {
                layer.msg(
                        res.message, {
                            icon: 5
                        }
                    );
            }
        });
        return false;
    });

    $("#closeDlg").click(function (){
        // iframe 页面关闭 添加parent
        parent.layer.closeAll();
    })

});